<?php

class Com_Basil_DynamicData_ArrayCollection extends Com_Basil_DynamicData_Collection {

	private $data;
	private $currentIdx;
	
	public function __construct($jsonObj) {
		$this->data = $jsonObj['data'];
		$this->currentIdx = 0;
	}
	
	public function hasNext() {
		return ($this->currentIdx < count($this->data));	
	}
		
 	public function next() {
 		if (!$this->hasNext()) {
 			return null;
 		}
 		return $this->data[$this->currentIdx++];
 	}
 	
 	public function reset() {
 		$this->currentIdx = 0;
 	}
 	
 	public function addToCollection($object) {
 		if (!is_a($object, 'Com_Basil_DynamicData_Object')) {
 			$objectKey = $object;
 		} else {
 			$objectKey = $object->getKey();
 		} 		
 		$this->data[] = $objectKey;	
 	}
 	public function removeFromCollection($object) {
 		//unfortunately we have to go through the array, i think
 		if (!is_a($object, 'Com_Basil_DynamicData_Object')) {
 			$objectKey = $object;
 		} else {
 			$objectKey = $object->getKey();
 		}
 		foreach($this->data as $key => $value) {
 			if ($value == $objectKey) {
 				unset($this->data[$key]);	
 			}
 		}
 		//re-index
 		$this->data = array_values($this->data);
 	}
 	
 	public function store() {
 		$jsonObj = array('type'=>'small', 'data'=>$this->data);
 		return Zend_Json::encode($jsonObj);	
 	}
	
}

?>